//
//  LZCheckCouponAndGrouponPresenter.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/12/7.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZCheckCouponAndGrouponPresenter.h"
#import "NetworkServiceManager.h"
#import "MJExtension.h"
#import "LZUserModel.h"
#import "LZPromotionStatusModel.h"

extern NSString *customer;

static NSString *kCheckCouponUrl = @"/clientcustomerCoupons/userCouponCheck";  // 检查--用户是否已经抢过优惠卷
static NSString *kCheckGrouponUrl = @"/clientgrouponUser/userGrouponCheck";    // 检查--用户是否已经参加过团购
static NSString *kCheckUsedCouponAndGrouponUrl = @"/clientCouponAndGroupon/isUsedCouponAndGroupon";    // 三小时内是否已使用过优惠券或团购

@implementation LZCheckCouponAndGrouponPresenter

/**
 检查--用户是否已经抢过优惠卷
 
 @param promotionId 优惠Id
 */
+ (void)checkCouponWithPromotionId:(NSString *)promotionId ResultSuccess:(void (^)(id))successBlock ResultFailed:(void (^)(NSString *))failedBlock {
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kCheckCouponUrl];
    NSDictionary *dic = @{
                          @"promotionId":promotionId,
                          @"token":[LZUserModel Instance].userToken
                          };
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            LZPromotionStatusModel *model = [LZPromotionStatusModel mj_objectWithKeyValues:resultData[@"data"]];

            if (successBlock) {
                successBlock(model);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}

/**
 检查--用户是否已经参加过团购
 
 @param promotionId 优惠Id
 */
+ (void)checkGrouponWithPromotionId:(NSString *)promotionId ResultSuccess:(void (^)(id))successBlock ResultFailed:(void (^)(NSString *))failedBlock {
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kCheckGrouponUrl];
    NSDictionary *dic = @{
                          @"promotionId":promotionId,
                          @"token":[LZUserModel Instance].userToken
                          };
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            LZPromotionStatusModel *model = [LZPromotionStatusModel mj_objectWithKeyValues:resultData[@"data"]];
            
            if (successBlock) {
                successBlock(model);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(error.description);
        }
    }];
}


/**
 三小时内是否已使用过优惠券或团购
 
 */
+ (void)checkUsedCouponAndGrouponResultSuccess:(void(^)(id result))successBlock ResultFailed:(void(^)(NSString *remind))failedBlock {
    
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kCheckUsedCouponAndGrouponUrl];
    NSDictionary *dic = @{
                          @"userId":[LZUserModel Instance].userId
                          };
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            
            if (successBlock) {
                successBlock(resultData[@"data"][@"isHaveUsedCouponOrGroupon"]);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
    
}

@end
